package com.cwt_taxi_xa.protocol.voice;

import android.util.Log;

/**
 * Created by unid on 2019/9/5 17:58
 * 0x8BF4协议处理
 */
public class ResponseState {

    public static final int RESULT_STATE_SUCCESS = 0;
    public static final int RESULT_STATE_DEVICE  = 1;
    public static final int RESULT_STATE_FILE_ILLEGAL = 2;
    public static final int RESULT_STATE_LATEST_VERSION = 3;
    public static final int RESULT_STATE_CONNECT_ERROR = 4;
    public static final int RESULT_STATE_LOG_IN_ERROR = 5;
    //06 升级文件错误[没有找到文件]
    public static final int RESULT_STATE_FILE_ERROR = 6;
    //07 ftp服务器错误[下载文件出错,或者下载文件被打断]
    public static final int RESULT_STATE_SERVER_ERROR = 7;
    public static final int RESULT_STATE_CRC_FAILED = 8;
    public static final int RESULT_STATE_PROGRESS = 9;
    public static final int RESULT_STATE_UNKNOWN = 0x0A;
    public static final int RESULT_STATE_ACC_OFF = 0x0B;

    private int resultState = RESULT_STATE_UNKNOWN;

    private int seq = -1;
    private int deviceID = 0;
    private String version = "";


    ResponseStateListener mResponseStateListener;

    public void setSeq(int seq) {
        this.seq = seq;
    }

    public int getSeq() {
        return seq;
    }

    public void setConfig(int seq, int deviceVersion, String version){
        this.seq = seq;
        this.deviceID = deviceVersion;
        this.version = version;
    }

    public void setResponseStateListener(ResponseStateListener mResponseStateListener) {
        this.mResponseStateListener = mResponseStateListener;
    }

    public int getResultState() {
        return resultState;
    }

    public void setResultState(int resultState) {
        this.resultState = resultState;
        //00 成功
        //01 存储设备故障
        //02 升级文件不合法
        //03 已是最新版本
        //04 ftp连接失败
        //05 ftp登录失败
        //06 升级文件错误
        //07 ftp服务器错误
        //08 升级文件校验失败
        //09 当前有任务正在进行
        //0A 未知错误
        //0B ACCOFF 不升级
        if(this.resultState != RESULT_STATE_PROGRESS){
            dispatchState();
        }
    }



    public void dispatchState(){
        Log.d("ResponseState","dispatchState seq:" + seq + " deviceID:" + deviceID + " version:" + version);
        if(mResponseStateListener != null && seq > 0){
            mResponseStateListener.onResponseState(seq,deviceID,version,resultState);
            seq = -1;
        }
        //mResponseStateListener = null;
    }


    public interface ResponseStateListener{
        void onResponseState(int seq,int deviceID,String version,int state);
    }
}
